package com.mc.moring.util;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

public class IntentUtils {

    public static void startBrowser(Context context, String url) {
        Intent intent = new Intent();
        intent.setAction("android.intent.action.VIEW");
        Uri content_url = Uri.parse(url);
        intent.setData(content_url);
        context.startActivity(intent);
    }

    public static void openMail(Activity activity, String emailValue) {
        Uri uri = Uri.parse("mailto:" + "");
        List<ApplicationInfo> applicationInfoList = activity.getPackageManager().getInstalledApplications(PackageManager.MATCH_UNINSTALLED_PACKAGES);//获取手机应用所有包名
        List<String> tempPkgNameList = new ArrayList<>();
        List<Intent> emailIntents = new ArrayList<>();
        for (ApplicationInfo info : applicationInfoList) {
            String pkgName = info.packageName;
            if (!tempPkgNameList.contains(pkgName)) {
                tempPkgNameList.add(pkgName);
                Intent intent = activity.getPackageManager().getLaunchIntentForPackage(pkgName);
                emailIntents.add(intent);
            }
        }
        if (!emailIntents.isEmpty()) {
            String[] email = {emailValue};
            Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
            intent.putExtra(Intent.EXTRA_EMAIL, email);
            intent.putExtra(Intent.EXTRA_SUBJECT, "");
            intent.putExtra(Intent.EXTRA_TEXT, ""); // 正
            Intent chooserIntent = Intent.createChooser(intent, "feedback");
            if (chooserIntent != null) {
                activity.startActivity(chooserIntent);
            } else {
                Log.d("openEmail", "no email type 1");
            }
        } else {
            Log.d("openEmail", "no email type 2");
        }
    }
}
